package com.ruoyi.system.utils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.google.gson.JsonParser;
import com.lark.oapi.Client;
import com.lark.oapi.core.utils.Jsons;
import com.lark.oapi.service.im.v1.model.CreateMessageReq;
import com.lark.oapi.service.im.v1.model.CreateMessageReqBody;
import com.lark.oapi.service.im.v1.model.CreateMessageResp;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.domain.SysExamine;
import com.ruoyi.system.domain.SysFlyingBook;
import com.ruoyi.system.mapper.SysExamineMapper;
import com.ruoyi.system.mapper.SysFlyingBookMapper;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Author Yin
 * @Date 2024/11/30 17:06
 * @Version 1.0
 */
@Component
public class MessageNotification {

    private static SysFlyingBookMapper sysFlyingBookMapper;

    @Autowired
    public void setUniDepartOrderMapper(SysFlyingBookMapper sysFlyingBookMapper) {
        MessageNotification.sysFlyingBookMapper = sysFlyingBookMapper;
    }


    @SneakyThrows
    public static void Message(String name,Integer threshold){
        // 获取查询检测配置数据
        SysFlyingBook sysFlyingBook = sysFlyingBookMapper.selectOne(null);
        // 判断是否为空
        if(Objects.isNull(sysFlyingBook)){
            throw new ServiceException("未获取到飞书配置数据");
        }
        RestTemplate restTemplate = new RestTemplate();

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON); // 设置为application/json

        // 创建一个空的Map来存储请求参数
        Map<String, Object> params = new LinkedHashMap<>();
        params.put("msg_type", "text");

        // 将消息内容构建为嵌套的 JSON 对象，包含 text 字段
        Map<String, String> content = new LinkedHashMap<>();
        content.put("text", "域名:"+name+"节点异常数" + threshold);

        // 将 content 加入到 params 中
        params.put("content", content);

        // 将参数转换为 JSON 字符串
        String jsonBody = new JSONObject(params).toString();

        // 创建 HttpEntity，包含请求头和请求体
        HttpEntity<String> entity = new HttpEntity<>(jsonBody, headers);

        // 执行 POST 请求
        String body = restTemplate.exchange(
                sysFlyingBook.getReceiveId(), // 您的 API 端点
                HttpMethod.POST, // 修改为 POST 方法
                entity,
                String.class).getBody(); // 响应体将被转换为 String 类型

        // 解析返回数据
        Map<String, Object> parse = (Map<String, Object>) JSON.parse(body);
        //判断是否成功
        if(!parse.get("msg").equals("success")){
            throw new ServiceException(parse.get("msg").toString());
        }
    }

}
